﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OkTool.Util
{
    /// <summary>
    /// Dictionary相关工具类
    /// </summary>
    public class DictionaryUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="K"></typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="mapList"></param>
        /// <returns></returns>
        public static Dictionary<K, List<V>> ToListMap<K, V>(IEnumerable<Dictionary<K, V>> mapList)
        {
            var resultMap = new Dictionary<K, List<V>>();

            if (mapList == null || !mapList.Any())
            {
                return resultMap;
            }

            foreach (var map in mapList)
            {
                foreach (var kvp in map)
                {
                    if (!resultMap.ContainsKey(kvp.Key))
                    {
                        resultMap[kvp.Key] = new List<V>();
                    }
                    resultMap[kvp.Key].Add(kvp.Value);
                }
            }

            return resultMap;
        }

        /// <summary>
        /// 集合转Map
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">key</param>
        /// <param name="valueSelector">value</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static Dictionary<TKey, TValue> ListToDictionary<TSource, TKey, TValue>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)
        {
            if (source == null) throw new ArgumentNullException(nameof(source));
            if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
            if (valueSelector == null) throw new ArgumentNullException(nameof(valueSelector));

            return source.ToDictionary(keySelector, valueSelector);
        }
        /// <summary>
        /// 字典转换成formData/Url
        /// </summary>
        /// <param name="dictionary"></param>
        /// <returns>a=1&amp;b=2</returns>
        public static string Dictionary2FormData(Dictionary<string, string> dictionary)
        {
            return string.Join("&", dictionary.Select(kvp => $"{kvp.Key}={kvp.Value}"));
        }

        /// <summary>
        /// formData/Url
        /// </summary>
        /// <param name="formData">a=1&amp;b=2</param>
        /// <returns></returns>
        public static Dictionary<string, string> FormData2Dictionary(string formData)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string[] pairs = formData.Split('&');
            foreach (string pair in pairs)
            {
                string[] keyValue = pair.Split('=');
                if (keyValue.Length == 2)
                {
                    dict[keyValue[0]] = keyValue[1];
                }
            }
            return dict;
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static bool IsEmpty(Dictionary<object, object> dic)
        {
            return dic == null || dic.Count == 0;
        }

    }
}
